package com.zhou.bjgl.bjgl.business_web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhou.bjgl.bjgl.business_web.entity.BCourse;
import com.zhou.bjgl.bjgl.business_web.entity.wx.WxUser;
import com.zhou.bjgl.bjgl.business_web.mapper.WxUserMapper;
import com.zhou.bjgl.bjgl.business_web.service.CommonService;
import com.zhou.bjgl.bjgl.exception.BwException;
import com.zhou.bjgl.bjgl.exception.BwExceptionEnum;
import com.zhou.bjgl.bjgl.myAnnotations.UserLoginToken;
import com.zhou.bjgl.bjgl.req.BaseModel;
import com.zhou.bjgl.bjgl.req.b_course.BCourseQueryReq;
import com.zhou.bjgl.bjgl.req.b_course.BCourseAddReq;
import com.zhou.bjgl.bjgl.resp.BCourseResp;
import com.zhou.bjgl.bjgl.resp.PageResp;
import com.zhou.bjgl.bjgl.resp.common.ApiRestResponse;
import com.zhou.bjgl.bjgl.business_web.service.BCourseService;
import com.zhou.bjgl.bjgl.thread.TimerThread;
import com.zhou.bjgl.bjgl.util.DateUtil;
import com.zhou.bjgl.bjgl.util.MyTreadPool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * <p>
 * 课堂管理
 * </p>
 *
 * @author jz
 * @since 2021-07-04
 */
@RestController
@RequestMapping("/bjgl/b-course")
@Api("课程管理")
public class BCourseController extends BaseModel {

    @Autowired
    private BCourseService bCourseService;

    @Resource
    private CommonService commonService;

    @Resource
    private WxUserMapper wxUserMapper;

    @ApiOperation("生成课程并返回课程码")
    @PostMapping("/add")
    @UserLoginToken
    public ApiRestResponse add(@RequestBody BCourseAddReq req) {
        req.setCreateUser(threadLocal.get().getId());
        String code = bCourseService.add(req);

        try {
            QueryWrapper<WxUser> wxUserQueryWrapper = new QueryWrapper<>();
            wxUserQueryWrapper.eq("user_id",threadLocal.get().getId());
            WxUser wxUser = wxUserMapper.selectOne(wxUserQueryWrapper);
            if (ObjectUtils.isEmpty(wxUser)) {
                throw new BwException(BwExceptionEnum.WE_UER_NULL);
            }
            //通过线程池，启动线程后台执行定时任务
            MyTreadPool.timingThreadPool().execute(new TimerThread(req.getCourseName(),wxUser.getOpenId(),req.getMinutes(), bCourseService, commonService,code));
            return ApiRestResponse.success(code);
        } catch (Exception e) {
            e.printStackTrace();
            QueryWrapper<BCourse> bCourseQueryWrapper = new QueryWrapper<>();
            bCourseService.remove(bCourseQueryWrapper.eq("course_code",code));
        }
        return ApiRestResponse.error(BwExceptionEnum.SYSTEM_EXCEPTION);
    }

    @ApiOperation("获取当前签到内容")
    @GetMapping("/current")
    @UserLoginToken
    public ApiRestResponse getCurrent() {
        QueryWrapper<BCourse> bCourseQueryWrapper = new QueryWrapper<>();
        bCourseQueryWrapper.eq("create_user", threadLocal.get().getId()).eq("status", 0).eq("del_status",0);
        BCourse course = bCourseService.getOne(bCourseQueryWrapper);
        return ApiRestResponse.success(course);

    }

    @ApiOperation("获取签到课程列表")
    @PostMapping("/list")
    @UserLoginToken
    public ApiRestResponse list(@RequestBody BCourseQueryReq req) {

        if (!ObjectUtils.nullSafeEquals(threadLocal.get().getPower(), 0)) {
            req.setCreateUser(threadLocal.get().getId());
        }
        if (threadLocal.get().getPower() == 1) {
            req.setCreateUser(null);
        }
        PageResp<BCourseResp> courseList = bCourseService.getCourseList(req);
        return ApiRestResponse.success(courseList);
    }

    @ApiOperation("删除课程签到")
    @PostMapping("/delete")
    @UserLoginToken
    public ApiRestResponse delete(@RequestBody BCourseQueryReq bCourseQueryReq) {
        //调用service层进行删除
        bCourseService.delete(bCourseQueryReq);
        return ApiRestResponse.success();
    }
}

